import 'package:flutter/material.dart';
import '../../helper/Helper.dart';
import '../../pages/shipuse/shipuse_authorty.dart';
import '../../widgets/inputWidget.dart';
import '../../model/shipuse.dart';
import '../../helper/uidata.dart';

class ShipUseHuiZhiAddPage extends StatefulWidget {
  @override
  ShipUseHuiZhiAddPage(this.s1, this.added);
  final ShipUse s1;
  final VoidCallback added;
  @override
  ShipUseHuiZhiAddPageState createState() => new ShipUseHuiZhiAddPageState();
}

class ShipUseHuiZhiAddPageState extends State<ShipUseHuiZhiAddPage> {
  late ShipUse X1;

  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();
  final TextEditingController c3 = new TextEditingController();
  final TextEditingController c4 = new TextEditingController();
  final TextEditingController c5 = new TextEditingController();
  final TextEditingController c6 = new TextEditingController();
  final TextEditingController c7 = new TextEditingController();
  final TextEditingController c8 = new TextEditingController();
  final TextEditingController c9 = new TextEditingController();
  final TextEditingController c10 = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    X1 = widget.s1;

    c1.text = X1.ChuanBo2;
    c2.text = X1.UseTime2;
    c3.text = X1.Peoples.toString();
    c4.text = X1.Length.toString();
    c5.text = X1.ManaName2;
    c6.text = X1.CZName2;
    c7.text = X1.UsePath2;
    c8.text = X1.TianQi;
    c9.text = X1.HangXing;

    if (c1.text.length < 2) c1.text = X1.ChuanBo;
    if (c2.text.length < 2) c2.text = X1.UseTime;
    if (c5.text.length < 2) c5.text = X1.ManaName;
    if (c6.text.length < 2) c6.text = X1.CZName;

    if (c5.text.length < 2) c5.text = UIData.CurrentUser!.Name;
  }

  upload(BuildContext context) async {
    if (X1.State < 4) {
      return;
    }

    X1.ChuanBo2 = c1.text;
    X1.UseTime2 = c2.text;
    X1.Peoples = int.parse(c3.text);
    X1.Length = double.tryParse(c4.text)!;
    X1.ManaName2 = c5.text;
    X1.CZName2 = c6.text;
    X1.UsePath2 = c7.text;
    X1.TianQi = c8.text;
    X1.HangXing = c9.text;

    X1.HZTM = Helper.DateTimeNowString();
    X1.State = 6;
    X1.CunDang = UIData.CurrentUser!.Name;
    await ShipUse.Update(X1);
    // widget.added();
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: new AppBar(
        // title: bottomTabs[currentIndex].title,
        title: Text("航行情况填报"),

        // leading: Icon(Icons.menu),
      ),
      body: ShipUseHuiZhiAddBody(context),
    );
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];

    list1.add(InputWidget.inCard(ShipUseAuthority.GetShipTitle(X1)));

    list2.add(InputWidget.getInput("航行船只", c1));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("航行时段", c2));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("乘船人次", c3));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("航行公里", c4));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("船艇管理人员", c5));

    list1.add(InputWidget.inCard2(list2));

    list3.add(InputWidget.getInput2("船长及船员", c6));
    list3.add(Divider(
      height: 0.0,
    ));
    list3.add(InputWidget.getInput2("航行路线", c7));
    list3.add(Divider(
      height: 0.0,
    ));
    list3.add(InputWidget.getInput2("天气及库面情况", c8));
    list3.add(Divider(
      height: 0.0,
    ));
    list3.add(InputWidget.getInput2("航行情况", c9));

    list1.add(InputWidget.inCard2(list3));
    list1.add(
        InputWidget.inCard(InputWidget.getButton("保存", () => upload(context))));
    //list1.add(getCLWidget(s1.State==6, Helper.DateTimeFormat(s1.HZQMTM), s1.HZQM));
    return list1;
  }

  ShipUseHuiZhiAddBody(BuildContext context) => SingleChildScrollView(
        child: ShipUseHuiZhiAddFields(context),
      );

  ShipUseHuiZhiAddFields(BuildContext context) {
    return Container(child: Column(children: getWidgetList(context)));
  }
}
